<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="网点code" prop="marketName">
        <el-input
          v-model="queryParams.outletsCode"
          placeholder="请输入网点code"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="选商名称" prop="marketType">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入选商名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="洽谈进度" prop="province">
        <el-select
          v-model="queryParams.talkProgress"
          placeholder="请选择洽谈进度"
        >
          <el-option
            v-for="item in progressData"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="洽谈方式" prop="city">
        <el-select v-model="queryParams.talkType" placeholder="请选择洽谈方式">
          <el-option
            v-for="item in methodData"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="合作店面类型" prop="brand">
        <el-select
          v-model="queryParams.storeType"
          placeholder="请选择合作店面类型"
        >
          <el-option
            v-for="item in storeData"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          size="small"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          size="small"
        >
          <el-option label="新建" value="0"></el-option>
          <el-option label="提交" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预计签约时间" prop="createTime">
        <el-date-picker
          v-model="createTime"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable
          size="small"
          style="width: 250px"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      border
      stripe
      :data="franchiseList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="网点Code"
        align="center"
        prop="outletsCode"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        label="选商名称"
        align="center"
        prop="customerName"
        width="120"
      />
      <el-table-column
        label="洽谈进度"
        align="center"
        show-overflow-tooltip
        width="180"
      >
        <template slot-scope="scope">
          <div v-for="item in progressData" :key="item.dictValue">
            <span v-if="scope.row.talkProgress == item.dictValue">{{
              item.dictLabel
            }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="洽谈方式"
        align="center"
        show-overflow-tooltip
        width="180"
      >
        <template slot-scope="scope">
          <div v-for="item in methodData" :key="item.dictValue">
            <span v-if="scope.row.talkType == item.dictValue">{{
              item.dictLabel
            }}</span>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="年龄" align="center" prop="city" /> -->
      <el-table-column
        label="洽谈内容"
        align="center"
        prop="talkInfo"
        width="180"
      />
      <el-table-column
        label="合作店面类型"
        align="center"
        show-overflow-tooltip
        width="180"
      >
        <template slot-scope="scope">
          <div v-for="item in storeData" :key="item.dictValue">
            <span v-if="scope.row.storeType == item.dictValue">{{
              item.dictLabel
            }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="预计签约时间"
        align="center"
        prop="planSigningTime"
        show-overflow-tooltip
        width="180"
      />

      <el-table-column
        label="终止原因"
        align="center"
        prop="suspendReason"
        show-overflow-tooltip
      />

       <el-table-column
        label="创建人"
        align="center"
        prop="custName"
        show-overflow-tooltip
      />

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'">新建</el-tag>
          <el-tag v-if="scope.row.status === '1'" type="warning">提交</el-tag>
        </template>
      </el-table-column>


    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改空白网点对话框 -->
  </div>
</template>

<script>
import { followupList } from "@/api/xdstore/branchInvest";

import requestCrm from "@/utils/requestCrm";
import { listRegionAll } from "@/api/region/region";

const statusOpts = [
  { label: "新建", value: "0" },
  { label: "已启动", value: "1" },
  { label: "已超期", value: "2" },
  { label: "已完成", value: "4" },
  { label: "已关闭", value: "3" },
];
const statusMap = statusOpts.reduce((res, item) => {
  res[item.value] = item.label;
  return res;
}, {});
export default {
  name: "Franchise",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 空白网点表格数据
      franchiseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outletsCode: "",
        marketName: "",
        createBy: "",
        talkProgress: "",
        talkType: "",
        storeType: "",
        customerName: "",
      },
      createTime: "",
      closingTime: "",
      progressData: [],
      methodData: [],
      storeData: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("negotiation_progress").then((response) => {
      this.progressData = response.data;
    });
    this.getDicts("negotiation_method").then((response) => {
      console.log(response, "返回结果");

      this.methodData = response.data;
    });
    this.getDicts("COOPERATE_STORE_TYPE").then((response) => {
      console.log(response, "返回结果");

      this.storeData = response.data;
    });
  },
  filters: {
    statusFilter(val) {
      if (!val) return "无效的状态";
      return statusMap[val] || "未知";
    },
  },
  methods: {
    /** 查询空白网点列表 */
    getList() {
      this.loading = true;
      let params = { ...this.queryParams };
      if (this.createTime) {
        console.log(this.createTime);
        params.startPlanSigningTime = this.createTime[0];
        params.endPlanSigningTime = this.createTime[1];
      }

      console.log(params.startCreateDate);
      followupList(params).then((response) => {
        this.franchiseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    selectProvinceFun(regionName) {
      const that = this;
      that.form.city = "";
      that.form.area = "";
      const r = this.provinceOptions.find((p) => p.regionName === regionName);
      that.getCityOptions(r.id);
    },
    selectCityFun(regionName) {
      this.form.area = "";
      const r = this.cityOptions.find((p) => p.regionName === regionName);
      this.getDistrictOptions(r.id);
    },
    getProvinceOptions(PID) {
      const that = this;
      listRegionAll({ parentId: "1" }).then((response) => {
        that.provinceOptions = response.data || [];
      });
    },
    getCityOptions(PID, city) {
      const that = this;
      listRegionAll({ parentId: PID }).then((response) => {
        that.cityOptions = response.data || [];
        if (city) {
          const a = that.cityOptions.find((c) => c.regionName === city);
          console.log("city", a);
          if (a) {
            that.getDistrictOptions(a.id);
          }
        }
      });
    },
    getDistrictOptions(CID) {
      const that = this;
      listRegionAll({ parentId: CID }).then((response) => {
        that.districtOptions = response.data || [];
      });
    },
 
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getBrandOptions() {
      requestCrm({
        url: "WxService?method=crmGetBrandAllList",
        method: "post",
        data: {},
      }).then((res) => {
        this.brandOptions = res.data;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        outletsCode: "",
        marketName: "",
        createBy: "",
        talkProgress: "",
        talkType: "",
        storeType: "",
        customerName: "",
      };
      this.createTime = "";
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加空白网点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getFranchise(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改空白网点";
        const { province, city } = this.form;
        const p = this.provinceOptions.find((p) => p.regionName === province);
        if (p) {
          console.log("update", city);
          this.getCityOptions(p.id, city);
        }
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateFranchise(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addFranchise(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除空白网点编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delFranchise(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有空白网点数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportFranchise(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>